/**
 * Created by liguigong on 2018/8/21.
 */

//1.传一个时间戳,得到具体的时间
function timestampToTime(timestamp) {
    //如果后台传递过来的时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var date = new Date(parseInt(timestamp*1000));
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes())+ ':';
    s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m+s;
}
console.log(timestampToTime('1534817768')); //2018-08-21 10:16:08

//2.将日期格式转换成unix时间戳传递给后台
function timeToTimestamp (time) {
    var date = new Date(time);
    return parseInt(date.getTime())/1000;
}

var time = '2018-08-21 10:34:32';
console.log(timeToTimestamp(time));


/**
 * 3.0
 * 传入一个已经过去了的时间点的时间戳13位[毫秒](也就是时间戳小于当前时间的时间戳)
 * 返回 '刚刚','几分钟前','几小时前','几天前','几周前','几月前' 如果大于月的话就会显示为几年几月几日(2015-02-26)
 * eg:  var zhi = timeago('1424966376000'); 返回的zhi就可以显示到页面了
 */
function timeago (dateTimeStamp) {
    var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
    var hour = minute * 60;
    var day = hour * 24;
    var week = day * 7;
    var month = day * 30;
    var now = new Date().getTime();   //获取当前时间毫秒
    var diffValue = now - dateTimeStamp;//时间差

    if (diffValue < 0) {
        return '传入的时间戳大于当前时间戳';
    }
    var minC = diffValue / minute;  //计算时间差的分，时，天，周，月
    var hourC = diffValue / hour;
    var dayC = diffValue / day;
    var weekC = diffValue / week;
    var monthC = diffValue / month;
    if (monthC >= 1 && monthC <= 3) {
        result = " " + parseInt(monthC) + "月前"
    } else if (weekC >= 1 && weekC <= 3) {
        result = " " + parseInt(weekC) + "周前"
    } else if (dayC >= 1 && dayC <= 6) {
        result = " " + parseInt(dayC) + "天前"
    } else if (hourC >= 1 && hourC <= 23) {
        result = " " + parseInt(hourC) + "小时前"
    } else if (minC >= 1 && minC <= 59) {
        result = " " + parseInt(minC) + "分钟前"
    } else if (diffValue >= 0 && diffValue <= minute) {
        result = "刚刚"
    } else {
        var datetime = new Date();
        datetime.setTime(dateTimeStamp);
        var Nyear = datetime.getFullYear();
        var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
        var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
        var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
        var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
        var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
        //这里如果需要精确到时分秒可以加上(这里我没有加)
        result = Nyear + "-" + Nmonth + "-" + Ndate
    }
    return result;
}



//4.从url中获取值
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURI(r[2]);
    }
    return null;
}

//例子,比如获取地址栏中的url的值
getQueryString('username');


// 5.根据对象的某一属性进行排序的js代码（如：name,age）
function sortByKey(array,key){
    return array.sort(function(a,b){
        var x=a[key];
        var y=b[key];
        return ((x<y)?-1:((x>y)?1:0));
    });
}

//实例
students = [
    {name:'jspang',age:32},
    {name:'Panda',age:30},
    {name:'PanPa',age:21},
    {name:'King',age:45}
];

var result = sortByKey(students,'age');  //result为排序好的

console.log(result);











